package cn.lzj.test;

import cn.lzj.TestDomain;
import cn.lzj.pet.domain.Pet;
import cn.lzj.pet.mapper.PetMapper;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

public class FreeMarker extends TestDomain {
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
    @Autowired
    private PetMapper petMapper;
    @Value("${templatedir}")
    private String templatePath;
    @Value("${htmldir}")
    private String htmlPath;

    @Test
    public void test() throws Exception {
        Configuration config = freeMarkerConfigurer.getConfiguration();
        config.setDefaultEncoding("UTF-8");
        config.setDirectoryForTemplateLoading(new File((templatePath)));
        Template template = config.getTemplate("petDetail.ftl");
        Pet pet = petMapper.loadById(13L);
        FileOutputStream fos = new FileOutputStream(new File(htmlPath, "petDetail.html"+pet.getId()+".html"));
        OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
        template.process(pet, osw);
    }
}
